package cn.aws360.boot.system.starter.controller;

import cn.aws360.boot.base.api.annotation.OpsLog;
import cn.aws360.boot.base.api.constant.enums.OpsTypeEnum;
import cn.aws360.boot.base.api.pojo.response.ResultModel;
import cn.aws360.boot.base.api.utils.ServletUtils;
import cn.aws360.boot.data.api.pojo.request.PageQuery;
import cn.aws360.boot.data.api.pojo.response.PageResult;
import cn.aws360.boot.system.api.pojo.request.SysLogsQuery;
import cn.aws360.boot.system.api.pojo.response.SysLogsModel;
import cn.aws360.boot.system.biz.service.SysLogsService;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import static cn.aws360.boot.system.api.constant.SysConstant.*;

/**
 * @author junnan
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/sys/logs")
@ApiSupport(order = MODULE_LOGS_CODE)
@Api(value = MODULE_LOGS_NAME, tags = MODULE_LOGS_TAGS)
public class SysLogsController {

    private final SysLogsService logsService;

    @GetMapping("/index.json")
    @SaCheckPermission("sys:logs:query")
    @ApiOperation(value = "日志页面", hidden = true)
    public ResultModel<JSONObject> index() {
        return ResultModel.ok(ServletUtils.getPageJson());
    }

    @GetMapping("/page")
    @SaCheckPermission("sys:logs:query")
    @ApiOperation(value = "日志分页")
    @ApiOperationSupport(order = 1)
    public ResultModel<PageResult<SysLogsModel>> page(SysLogsQuery sysLogsQuery, PageQuery pageQuery) {
        return ResultModel.ok(logsService.findPage(sysLogsQuery, pageQuery));
    }

    @GetMapping("/{id}")
    @SaCheckPermission("sys:logs:detail")
    @ApiOperation(value = "日志详情")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParam(name = "id", value = "日志 id", dataTypeClass = Integer.class, required = true)
    public ResultModel<SysLogsModel> detail(@PathVariable Integer id) {
        return ResultModel.ok(logsService.detail(id));
    }

    @DeleteMapping("/{ids}")
    @SaCheckPermission("sys:logs:delete")
    @OpsLog(title = "删除日志", opsType = OpsTypeEnum.DELETE)
    @ApiOperation(value = "删除日志")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParam(name = "ids", value = "日志 ids", dataTypeClass = String.class, required = true)
    public ResultModel<Boolean> delete(@PathVariable String ids) {
        return ResultModel.ok(logsService.delete(ids));
    }

}
